const clouddb = require('@hw-agconnect/database-server/dist/index.js');
const agconnect = require('@hw-agconnect/common-server');
const path = require('path');

/*
    以下代码不需要同学们理解
    * 可以直接复制我的代码
*/
//TODO 将AGC官网下载的配置文件放入resources文件夹下并将文件名替换为真实文件名
const credentialPath = "resources/agc-apiclient-1405248561209182976-7361036096769759511.json";
// 修改为在管理台创建的存储区名称
let zoneName = "db"
// 修改为需要操作的对象
let objectName = 'user';

let logger

let mCloudDBZone

class CloudDBZoneWrapper {

    // AGC & 数据库初始化
    constructor(log) {
        logger = log;

        let agcClient;

        try {
            agcClient = agconnect.AGCClient.getInstance();
        } catch (error) {
            agconnect.AGCClient.initialize(agconnect.CredentialParser.toCredential(path.join(__dirname, credentialPath)));
            agcClient = agconnect.AGCClient.getInstance();
        }

        clouddb.AGConnectCloudDB.initialize(agcClient);

        const cloudDBZoneConfig = new clouddb.CloudDBZoneConfig(zoneName);

        const agconnectCloudDB = clouddb.AGConnectCloudDB.getInstance(agcClient);
        mCloudDBZone = agconnectCloudDB.openCloudDBZone(cloudDBZoneConfig);
    }

    async add(payload) {
        if (!mCloudDBZone) {
            logger.info("CloudDBClient is null, try re-initialize it");
            console.log("CloudDBClient is null, try re-initialize it")
            return;
        }
        try {
            // 构建数据库
            const user = clouddb.CloudDBZoneGenericObject.build(objectName);
            user.addFieldValue('name', payload.name);
            user.addFieldValue('email', payload.email);
            user.addFieldValue('uid', payload.uid);
            user.addFieldValue('avatar', payload.avatar);
            // 把数据插入到数据库
            return mCloudDBZone.executeUpsert(user);
        } catch (error) {
            logger.info('queryAll=>', error);
            console.log(error)
        }
    }

    async patch(payload) {
        if (!mCloudDBZone) {
            logger.info("CloudDBClient is null, try re-initialize it");
            console.log("CloudDBClient is null, try re-initialize it")
            return;
        }
        try {
            // 注意使用CloudDBZoneGenericObject进行构建
            const user = clouddb.CloudDBZoneGenericObject.build('user');
            user.addFieldValue('name', payload.name);
            user.addFieldValue('uid', payload.uid);
            user.addFieldValue('email', payload.email);
            user.addFieldValue('avatar', payload.avatar);
            return mCloudDBZone.executeUpsert(user);
        } catch (error) {
            console.log(error)
        }
    }

    async query(payload) {
        if (!mCloudDBZone) {
            logger.info("CloudDBClient is null, try re-initialize it");
            console.log("CloudDBClient is null, try re-initialize it")
            return;
        }
        try {
            const resp = await mCloudDBZone.executeQuery(
                clouddb.CloudDBZoneQuery.where(objectName)
                    .equalTo('uid', payload.uid)
            );
            return resp.getSnapshotObjects();
        } catch (error) {
            logger.info('queryAll=>', error);
            console.log(error)
        }
    }

    async queryAll() {
        if (!mCloudDBZone) {
            logger.info("CloudDBClient is null, try re-initialize it");
            console.log("CloudDBClient is null, try re-initialize it")
            return;
        }
        try {
            const resp = await mCloudDBZone.executeQuery(
                clouddb.CloudDBZoneQuery.where(objectName)
            );
            return resp.getSnapshotObjects();
        } catch (error) {
            logger.info('queryAll=>', error);
            console.log(error)
        }
    }
}

module.exports = CloudDBZoneWrapper;